package com.ruoyi.common.core.utils.facade;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.OaCandidateInfo;
import com.ruoyi.common.core.domain.OaResult;
import com.ruoyi.common.core.utils.AESUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fyw
 * @Title: OaBusinessFacade
 * @date 2020年12月29日16:41:23
 */
@Component
public class OaBusinessFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(OaBusinessFacade.class);
   // private static String baseUrl ="https://118.102.22.117:1080";
    private static String baseUrl =" https://ect.jsfund.cn:1085";

    protected static final String KEY = "TYUFD%^7gfdsDSFV#$@$^&HDFV23423SGDFV&*&@#$wretWE$%";
    protected static final String PROVIDER_CODE="DFRC";

   // @Value("${blockchain.nianshou}") 、、
   // private String baseUrl ;

    public static OaResult getRecruitRequest(){
        Map<String,Object> param = new HashMap<>();
        try {
            param.put("securityProCode", AESUtils.encrypt("DFRC",KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultProcess(baseUrl.concat("/entry/sendRecruitRequest"),param);
    }

    public static OaResult uploadZipFile(MultipartFile file){
        Map<String,Object> param = new HashMap<>();
        try {
            param.put("securityProCode", AESUtils.encrypt("DFRC",KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        param.put("file",file);
        return resultProcessFile(baseUrl.concat("/entry/uploadZipFile"),param);
    }

   public static OaResult uploadZipFile(File file,String processCode){
        Map<String,Object> param = new HashMap<>();
        try {
            param.put("securityProCode", AESUtils.encrypt("DFRC",KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        param.put("file",file);
       param.put("processCode",processCode);
        return resultProcessFile(baseUrl.concat("/entry/uploadZipFile"),param);
    }

    public static OaResult addCandidateInfo(List<OaCandidateInfo> candidateInfo){
        Map<String,Object> param = entityToMap(candidateInfo);
        try {
            param.put("securityProCode", AESUtils.encrypt("DFRC",KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultProcess1(baseUrl.concat("/entry/addCandidateInfo"),candidateInfo,param);
    }

    public static OaResult resultProcess(String url, Map<String,Object> params) {
        String result = null;
        try {
            params.put("providerCode","DFRC");
           // result= HttpUtil.post(url, params);

            String json =JSON.toJSONString(params);
            result= HttpRequest.post(url)
                    .body(json)
                    .execute().body();
            result= AESUtils.decrypt(result,KEY);
            if (null != result){
                OaResult oaResult = JSON.parseObject(result,OaResult.class);
              //  LOGGER.info("request url:{},params:{},result:{}",url,JSON.toJSONString(params),oaResult);
                return oaResult;
            }
        } catch (Exception e) {
            LOGGER.error("url:{},",url,e);
        }

        return null;
    }
    public static OaResult resultProcess1(String url,List<OaCandidateInfo> list, Map<String,Object> params) {
        String result = null;
        try {
            params.put("providerCode","DFRC");
            List<Map<String,Object>> listResult=new ArrayList<>();
            for (OaCandidateInfo o:list){
                Map<String, Object> oo= objectToMap(o);
                oo.put("graduateDate", DateUtils.dateTime(o.getGraduateDate()));
                oo.put("entryDate", DateUtils.dateTime(o.getEntryDate()));
                listResult.add(oo);
            }
            String aa= URLEncoder.encode(JSON.toJSONString(listResult),"GBK");
            params.put("addCandidateInfoParam",AESUtils.encrypt(aa,KEY));

            String json =JSON.toJSONString(params);
            result= HttpRequest.post(url)
                    .body(json)
                    .execute().body();
            result= AESUtils.decrypt(result,KEY);
            if (null != result){
                OaResult oaResult = JSON.parseObject(result,OaResult.class);
               // LOGGER.info("request url:{},params:{},result:{}",url,JSON.toJSONString(params),oaResult);
                return oaResult;
            }
        } catch (Exception e) {
            LOGGER.error("url:{},",url,e);
        }

        return null;
    }

    public static OaResult resultProcessFile(String url, Map<String,Object> params) {
        String result = null;
        try {
            params.put("providerCode","DFRC");
            result= HttpUtil.post(url, params);
            LOGGER.info(result);
          //  result= AESUtils.decrypt(result,KEY);
            if (null != result){
                OaResult oaResult = JSON.parseObject(result,OaResult.class);
                LOGGER.info("request url:{},params:{},result:{}",url,JSON.toJSONString(params),oaResult);
                return oaResult;
            }
        } catch (Exception e) {
            LOGGER.error("url:{},",url,e);
        }

        return null;
    }
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap();
        for (Field field : object.getClass().getDeclaredFields()){
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static void main(String[] args) {
        //File file=FileUtil.file("D:\\20210112135856东方融创.zip");
        //OaResult result= OaBusinessFacade.uploadZipFile(file);
        List<OaCandidateInfo> list=new ArrayList<>();
        OaCandidateInfo o=new OaCandidateInfo();
        o.setProcessCode("P591-201110-100");
        o.setCandidateName("张三");
        list.add(o);

        OaResult result=  addCandidateInfo(list);
        System.out.println(JSON.toJSONString(result));
    }

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if(obj == null)
            return null;

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter!=null ? getter.invoke(obj) : null;
            map.put(key, value);
        }

        return map;
    }

}

